/**
 * 
 */
package com.skood.security.service.impl;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.skood.common.ExecuteResult;
import com.skood.common.dao.CommonDAO;
import com.skood.common.service.AbstractService;
import com.skood.security.dao.UserDAO;
import com.skood.security.domain.User;
import com.skood.security.service.UserService;

/**
 * @author keege
 * @date 2013-9-9
 * 用户管理
 */
@Service("userService")
public class UserServiceImpl extends AbstractService<User> implements UserService{

	private static Logger LOG = LoggerFactory.getLogger(UserServiceImpl.class);
	@Resource(name="userDAO")
	private UserDAO userDAO;
	
	@Override
	protected CommonDAO<User> getDAO() {
		// TODO Auto-generated method stub
		return userDAO;
	}

	@Override
	public ExecuteResult<User> login(String username, String password) {

		ExecuteResult<User> executeResult = new ExecuteResult<User>();
		username = StringUtils.trimWhitespace(username);
		password = StringUtils.trimWhitespace(password);
		if(StringUtils.isEmpty(username)||StringUtils.isEmpty(password)){
			//用户名或密码不能为空
		}
		User user = userDAO.getUserByName(username);
		if(user == null){
			//用户不存在
			LOG.info("登录用户名不存在");
			executeResult.addErrorMessages("用户不存在");
			return executeResult;
		}
		if(!password.equals(user.getPassword())){
			//用户密码错误
			executeResult.addErrorMessages("用户密码错误");
			return executeResult;
		}
		executeResult.setResult(user);
		return executeResult;
	}

	
	@Override
	public ExecuteResult<User> createUser(User user) {
		// TODO Auto-generated method stub
		ExecuteResult<User> executeResult = new ExecuteResult<User>();
		if(user == null||StringUtils.isEmpty(user.getName())){
			LOG.info("创建用户的用户名不能为空");
			executeResult.addErrorMessages("创建用户的用户名不能为空");
			return executeResult;
		}
		User dbUser = userDAO.getUserByName(user.getName());
		if(dbUser != null){
			//用户名已经存在
			LOG.info("用户名已经存在");
			executeResult.addErrorMessages("用户名已经存在");
			return executeResult;
		}
		try{
			userDAO.save(user);
		}catch(RuntimeException e){
			LOG.info("创建用户失败{}",e.getMessage());
			executeResult.addErrorMessages("创建用户失败,code:20000");
			throw e;
		}
		executeResult.setResult(user);
		return executeResult;
	}

	public ExecuteResult<User> updateUser(User user){
		ExecuteResult<User> executeResult = new ExecuteResult<User>();
		if(user == null||user.getId()==null){
			LOG.info("要修改的用户信息无效");
			executeResult.addErrorMessages("要修改的用户信息无效");
			return executeResult;
		}
		if(StringUtils.isEmpty(user.getNickName())){
			LOG.info("用户姓名不能为空");
			executeResult.addErrorMessages("用户姓名不能为空");
			return executeResult;
		}
		
		try{
			Long num = userDAO.update(user);
			LOG.info("成功删除 数量:{}",num);
		}catch(RuntimeException e){
			LOG.info("修改用户信息失败{}",e.getMessage());
			executeResult.addErrorMessages("修改用户信息失败,code:20000");
			throw e;
		}
		executeResult.setResult(user);
		return executeResult;
	}
	/*@Override
	public ExecuteResult<User> doDelete(Long[] ids) {
		// TODO Auto-generated method stub
		ExecuteResult<User> executeResult = new ExecuteResult<User>();
		if(ArrayUtils.isEmpty(ids)){
			LOG.info("要删除的数据参数不能为空");
			executeResult.addErrorMessages("要删除的数据参数不能为空");
			return executeResult;
		}
		try{
			userDAO.delete(ids);
		}catch(RuntimeException e){
			LOG.error("删除数据异常,参数ids={},{}",ids,e.getMessage());
			throw e;
		}
		
		return executeResult;
	}*/
}
